package pers.qianyu.month_202104.date_20210401;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 1006. 笨阶乘
 * https://leetcode-cn.com/problems/clumsy-factorial/
 *
 * @author mizzle rain
 * @date 2021-04-01 11:34
 */
public class Clumsy {
    public int clumsy(int N) {
        if (N <= 1) return N;
        Deque<Integer> st = new ArrayDeque<>();
        st.addLast(N);
        for (int i = N - 1, t = 0; i >= 1; i--, t = (t + 1) % 4) {
            if (t == 0) {
                st.addLast(st.removeLast() * i);
            } else if (t == 1) {
                st.addLast(st.removeLast() / i);
            } else if (t == 2) {
                st.addLast(i);
            } else {
                st.addLast(-i);
            }
        }

        int res = 0;
        for (int x : st) res += x;
        return res;
    }
}
